package com.ibeetl.admin.aod.service;

import java.util.ArrayList;
import java.util.List;

import com.ibeetl.admin.aod.web.dto.AodIndustryDataExportDto;
import com.ibeetl.admin.aod.web.dto.AodIndustryDataNonDto;
import com.ibeetl.admin.aod.web.dto.rowmapper.AodIndustryDataExcelRowMapper;
import com.ibeetl.admin.aod.web.dto.rowmapper.AodIndustryDataNonRowMapper;
import com.ibeetl.admin.aod.web.query.AodIndustryDataExportQuery;
import com.ibeetl.admin.aod.web.query.AodIndustryDataNonQuery;
import com.ibeetl.admin.aod.web.query.AodIndustryDataQuery;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ibeetl.admin.core.util.PlatformException;

import com.ibeetl.admin.aod.dao.AodIndustryDataDao;
import com.ibeetl.admin.aod.entity.AodIndustryData;
import com.ibeetl.admin.core.service.BaseService;

/**
 * AodIndustryData Service
 */

@Service
@Transactional
public class AodIndustryDataService extends BaseService<AodIndustryData>{

    @Autowired
    protected AodIndustryDataDao aodIndustryDataDao;

    @Autowired
    @Qualifier("defaultJdbcTemplate")
    protected JdbcTemplate jdbcTemplate;

    @Autowired
    protected org.springframework.core.env.Environment environment;

    public PageQuery<AodIndustryData> queryByCondition(PageQuery query){
        PageQuery ret =  aodIndustryDataDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public void batchDelAodIndustryData(List<Long> ids){
        try {
            aodIndustryDataDao.batchDelAodIndustryDataByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除AodIndustryData失败", e);
        }
    }

    public List<AodIndustryData> queryListByCondition(AodIndustryDataQuery condition) {
        return aodIndustryDataDao.queryListByCondition(condition);
    }

    public void deleteOneById(Long id) {
        aodIndustryDataDao.deleteOneById(id);
    }

    /**
     * 导出行业数据的 -> 分页查询
     * @param query
     * @return
     */
    public PageQuery<AodIndustryDataExportDto> queryExportByCondition(PageQuery query){

        // 自己封装分页
        AodIndustryDataExportQuery aodIndustryDataExportQuery = (AodIndustryDataExportQuery) query.getParas();

        StringBuilder countSql = new StringBuilder(String.format(environment.getProperty("aod.dynamicCountQuerySql"), aodIndustryDataExportQuery.getTableName()));

        StringBuilder whereSql = new StringBuilder();

        List<Object> paramList = new ArrayList<>();

        if (StringUtils.isNotEmpty(aodIndustryDataExportQuery.getAccount())) {
            whereSql.append(" and t.account = ? ");
            paramList.add(aodIndustryDataExportQuery.getAccount());
        }
        if (StringUtils.isNotEmpty(aodIndustryDataExportQuery.getPhoneNumber())) {
            whereSql.append(" and t.phone_number = ? ");
            paramList.add(aodIndustryDataExportQuery.getPhoneNumber());
        }
        if (StringUtils.isNotEmpty(aodIndustryDataExportQuery.getProvince())) {
            whereSql.append(" and b.province = ? ");
            paramList.add(aodIndustryDataExportQuery.getProvince());
        }
        if (StringUtils.isNotEmpty(aodIndustryDataExportQuery.getCity())) {
            whereSql.append(" and b.city = ? ");
            paramList.add(aodIndustryDataExportQuery.getCity());
        }
        if (StringUtils.isNotEmpty(aodIndustryDataExportQuery.getCorp())) {
            whereSql.append(" and b.corp = ? ");
            paramList.add(aodIndustryDataExportQuery.getCorp());
        }
        if (StringUtils.isNotEmpty(aodIndustryDataExportQuery.getLevel())) {
            whereSql.append(" and t.level = ? ");
            paramList.add(aodIndustryDataExportQuery.getLevel());
        }

        if (null != aodIndustryDataExportQuery.getAddTimeMin() && null != aodIndustryDataExportQuery.getAddTimeMax()) {
            whereSql.append(" and t.add_time >= ? and t.add_time <= ? ");
            paramList.add(aodIndustryDataExportQuery.getAddTimeMin());
            paramList.add(aodIndustryDataExportQuery.getAddTimeMax());
        }

        countSql.append(whereSql);

        Long iCount = jdbcTemplate.queryForObject(countSql.toString(), paramList.toArray(), Long.class);

        query.setTotalRow(iCount);

        StringBuilder pageSql = new StringBuilder(String.format(environment.getProperty("aod.dynamicPageQuerySql"), aodIndustryDataExportQuery.getTableName()));
        whereSql.append(" limit ?,?");
        pageSql.append(whereSql);
        paramList.add((query.getPageNumber() - 1) * query.getPageSize());
        paramList.add(query.getPageSize());

        List<AodIndustryDataExportDto> resultList = jdbcTemplate.query(pageSql.toString(), paramList.toArray(), new AodIndustryDataExcelRowMapper());

        query.setList(resultList);

//        queryListAfter(query.getList());
        return query;
    }

    /**
     * 无行业数据的标签量 -> 分页查询
     * @param query
     * @return
     */
    public PageQuery<AodIndustryData> queryNonByCondition(PageQuery query){

        // 自己封装分页
        AodIndustryDataNonQuery aodIndustryDataNonQuery = (AodIndustryDataNonQuery) query.getParas();
        String statNonQuerySql = environment.getProperty("aod.statNonQuerySql");

        StringBuilder countSql = new StringBuilder(" select count(1) from ( " + statNonQuerySql);

        StringBuilder whereSql = new StringBuilder();

        List<Object> paramList = new ArrayList<>();

        if (StringUtils.isNotEmpty(aodIndustryDataNonQuery.getLabel())) {
            whereSql.append(" and  t.label = ? ");
            paramList.add(aodIndustryDataNonQuery.getLabel());
        }

        whereSql.append(" group by t.label order by labelNum desc,t.label ");
        countSql.append(whereSql + " ) f ");

        Long iCount = jdbcTemplate.queryForObject(countSql.toString(), paramList.toArray(), Long.class);

        query.setTotalRow(iCount);

        StringBuilder pageSql = new StringBuilder(statNonQuerySql);
        whereSql.append(" limit ?,?");
        pageSql.append(whereSql);
        paramList.add((query.getPageNumber() - 1) * query.getPageSize());
        paramList.add(query.getPageSize());

        List<AodIndustryDataNonDto> resultList = jdbcTemplate.query(pageSql.toString(), paramList.toArray(), new AodIndustryDataNonRowMapper());

        query.setList(resultList);

//        queryListAfter(query.getList());
        return query;
    }

}